﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LopService;
namespace eTMS.Controllers{
public class LopControllerBase : CommonController{
	public LopControllerBase()
		: base()
	{	
		this.dao = new LopServiceClient();
	}
	~LopControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private LopEntity oLop;	
	public LopEntity LopObject{
		get{
			return oLop;
		}
		set{
			oLop=value;
		}
	}
	protected LopServiceClient dao;
	public DataTable Table{
		get{			
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLop==null)return false;
		bool result=true;
		try{
			oLop = dao.SaveRet(oLop);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLop.Add(oLop);
		}
		return result;
	}
	public bool Update(){
		if(oLop==null)return false;
		return dao.Update(oLop);
	}
	public bool Delete(){
		if(oLop==null)return false;
		bool retvalue = dao.Delete(oLop);
		if(retvalue){			
			_ListLop.Remove(oLop);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 Lop_ID){
		bool retvalue = dao.DeleteByID(Lop_ID);		
		if(retvalue){
			LopEntity oDeleted=FindLop(Lop_ID);
			if(oDeleted!=null){
				_ListLop.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLop = new LopEntity();
	}
	//Check if exists MaLop
	public bool CheckExistsMaLop(object value){	
		return dao.SelectOneByMaLop(value.ToString()).MaLop!=null;
    }    
    //Copy data from data row in grid to object
    public LopEntity MapDataRowToObject(DataRow row){
		LopEntity o = new LopEntity();
		 o.MaLop=Convert.ToString(row["MaLop"]);
		 o.TenLop=Convert.ToString(row["TenLop"]);
		 if(!row.IsNull("SoSinhVienToiDa"))
			o.SoSinhVienToiDa=ConvertInt(row["SoSinhVienToiDa"]);
		 o.MoTaLop=Convert.ToString(row["MoTaLop"]);
		 if(!row.IsNull("GiaoVienPhuTrach_ID"))
			o.GiaoVienPhuTrach_ID=ConvertInt(row["GiaoVienPhuTrach_ID"]);
		 if(!row.IsNull("KhoaHoc_ID"))
			o.KhoaHoc_ID=(Int32)(row["KhoaHoc_ID"]);
		 if(!row.IsNull("NganhHoc_ID"))
			o.NganhHoc_ID=(Int32)(row["NganhHoc_ID"]);
		 if(!row.IsNull("Lop_ID"))
		    o.Lop_ID=(Int32)(row["Lop_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LopEntity o,DataRow row){
		row["MaLop"]=o.MaLop;
		row["TenLop"]=o.TenLop;
		row["SoSinhVienToiDa"]=o.SoSinhVienToiDa;
		row["MoTaLop"]=o.MoTaLop;
		row["GiaoVienPhuTrach_ID"]=o.GiaoVienPhuTrach_ID;
		row["KhoaHoc_ID"]=o.KhoaHoc_ID;
		row["NganhHoc_ID"]=o.NganhHoc_ID;
		row["Lop_ID"]=o.Lop_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LopObject,row);
    }
	public void LoadObject(Int32 Lop_ID){
		LopEntity oLoad=FindLop(Lop_ID);
		if(oLoad==null){
			oLop=this.LoadLop(Lop_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLop.IsNullLop_ID;
			if(!bNotIsNull){
				this.ListLop.Add(oLop);
			}
		}
		else{
			oLop=oLoad;
		}
	}
	#region CachedList Lop
	private static List<LopEntity> _ListLop=new List<LopEntity>();
    public List<LopEntity>ListLop{
		get{
			return _ListLop;
		}
	}
	public static List<LopEntity>ListLopStatic{
		get{
			return _ListLop;
		}
	}
	public LopEntity FindLop(Int32 Lop_ID){
		LopEntity oResult=this.ListLop.Find(delegate(LopEntity oFind){
			bool result=true; 
			result&=oFind.Lop_ID.Equals(Lop_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLop(bool clearCache){
		if(_ListLop.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LopServiceClient dao = new LopServiceClient();
		_ListLop=new List<LopEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public LopEntity LoadLop(Int32 Lop_ID){			
		LopEntity o=dao.SelectOne(Lop_ID);		
		return o;
	}
	#endregion
}
}
